package cn.szabadsag.org.service.impl;

import cn.szabadsag.base.service.impl.BaseServiceImpl;
import cn.szabadsag.org.domain.Department;
import cn.szabadsag.org.mapper.DepartmentMapper;
import cn.szabadsag.org.service.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author yangcong
 * @date 2022年03月02日 9:59
 * @since
 */
@Service
public class DepartmentServiceImpl extends BaseServiceImpl<Department> implements IDepartmentService {

    @Autowired
    private DepartmentMapper departmentMapper;

    /**
     * 新增
     * @param department
     */
    @Override
    @Transactional
    public void save(Department department) {
        System.out.println(department);
        departmentMapper.save(department);//获取新增的主键
        //dirPath 维护
        //1.判断 上级parent 是否为空
       /* if(department.getParent()==null){
            department.setDirPath("/"+department.getId());
        }else {
            //获取上级部门的dirPath
            Department parent = departmentMapper.loadById(department.getParent().getId());
            department.setDirPath(parent.getDirPath()+"/"+department.getId());
        }
        //更新数据
        departmentMapper.update(department);*/
        update(department);
    }

    /**
     * 修改
     * @param department
     */
    @Override
    @Transactional
    public void update(Department department) {
        //1.判断 上级parent 是否为空
        if(department.getDeptParent() == null){
            department.setDirPath("/"+department.getId());
        }else {
            //获取上级部门的dirPath
            Department parent = departmentMapper.loadById(department.getDeptParent().getId());
            department.setDirPath(parent.getDirPath()+"/"+department.getId());
        }
        departmentMapper.update(department);
    }

    /**
     * 批量删除
     * @param ids
     */
    @Override
    @Transactional
    public void patchDel(Long[] ids) {
        departmentMapper.patchDel(ids);
    }

    /**
     * 部门类树级关系
     * @return
     */
    @Override
    public List<Department> loadDeptTree() {
        return departmentMapper.loadDeptTree();
    }
}
